package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.service.IcdDiseaseHospitalService;

@Controller
public class IcdDiseaseHospitalController {

	@Autowired
	private IcdDiseaseHospitalService icdDiseaseHospitalService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/disease_hospital_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");
			pageParam.put("type", "1");
			pageParam.put("checkFlag", "1");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameSearch");
		String stdSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "stdSearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String categorySearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "categorySearch");
		String type = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "type");
		String checkFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "checkFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdDiseaseHospitalService.list(start, codeSearch, nameSearch, exactSearchFlag, type, checkFlag,
				countPerPageStr, stdSearch, categorySearch);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_hospital_change_disable.do")
	public ReturnData changeDisable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String disable = req.getParameter("disable");
		String diseaseName = req.getParameter("diseaseName");
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = icdDiseaseHospitalService.changeDisable(id, user, disable, diseaseName);
		return rtn;
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_hospital_export.do")
	public ReturnData export(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String chapterSequence = req.getParameter("chapterSequence");
		String chapterCode = req.getParameter("chapterCode");
		String chapterName = req.getParameter("chapterName");
		String sectionCode = req.getParameter("sectionCode");
		String sectionName = req.getParameter("sectionName");
		String gpCode = req.getParameter("gpCode");
		String gpAccessoryCode = req.getParameter("gpAccessoryCode");
		String gpName = req.getParameter("gpName");
		String suborderCode = req.getParameter("suborderCode");
		String suborderAccessoryCode = req.getParameter("suborderAccessoryCode");
		String suborderName = req.getParameter("suborderName");
		icdDiseaseHospitalService.export(resp, chapterSequence, chapterCode, chapterName, sectionCode, sectionName,
				gpCode, gpAccessoryCode, gpName, suborderCode, suborderAccessoryCode, suborderName);
		;
		return ReturnUtil.success();
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/icd/change_hospital_checkFlag.do")
	public ReturnData submitPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		User user = (User) req.getSession().getAttribute("user");

		Map<String, Object> map = req.getParameterMap();
		String[] dataIds = (String[]) map.get("dataId");
		String[] disable = (String[]) map.get("disable");
		try {
			return icdDiseaseHospitalService.submitChangeDisable(dataIds, disable, user);
		} catch (Exception e) {
			if (e.getMessage() != null && e.getMessage().startsWith("提交失败")) {
				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
				rc.setDesc(e.getMessage());
				return ReturnUtil.fail(rc);
			} else {
				throw e;
			}
		}
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_hospital_submit_edit.do")
	public ReturnData submitEdit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String diseaseName = req.getParameter("diseaseName");
		String diseaseCode = req.getParameter("diseaseCode");
		String std = req.getParameter("std");
		String disable = req.getParameter("disable");
		String category = req.getParameter("category") == null ? "" : req.getParameter("category");
		String modifyCode = req.getParameter("modifyCode") == null ? "" : req.getParameter("modifyCode");
		String similarDisease = req.getParameter("similarDisease") == null ? "" : req.getParameter("similarDisease");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");
		try {
			return icdDiseaseHospitalService.submitEdit(id, diseaseName, diseaseCode, std, disable, category, user,
					modifyCode, similarDisease);
		} catch (Exception e) {
			if (e.getMessage() != null && e.getMessage().startsWith("提交失败")) {
				ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
				rc.setDesc(e.getMessage());
				return ReturnUtil.fail(rc);
			} else {
				throw e;
			}
		}
	}

	@ResponseBody
	@RequestMapping(value = "/icd/disease_hospital_import.do")
	public ReturnData synonymImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return icdDiseaseHospitalService.hospitalImport(file, user);
	}

	@RequestMapping(value = "/icd/disease_hospital_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		String id = req.getParameter("id");
		return icdDiseaseHospitalService.preEdit(id);
	}
}
